package com.jmga.ecogasfinder.activities.maps.overlays;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
import com.jmga.ecogasfinder.GasStation;

public class GasStationOverlay extends ItemizedOverlay<OverlayItem> {

	private ArrayList<GasStation> gasStations = new ArrayList<GasStation>();
	private Location location;
	private Context mContext;
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	
	public GasStationOverlay(Drawable defaultMarker) {
		  super(boundCenterBottom(defaultMarker));
	}
	
	public GasStationOverlay(Drawable defaultMarker, Context context) {
		  super(boundCenterBottom(defaultMarker));
		  this.mContext = context;
		}

	public Location getLocation() {
		return this.location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public ArrayList<GasStation> getGasStations() {
		return this.gasStations;
	}

	public void setGasStations(ArrayList<GasStation> gasStations) {
		this.gasStations = gasStations;
	}

	public void addOverlay(OverlayItem overlay) {
		this.mOverlays.add(overlay);
	    populate();
	}
	
	@Override
	protected OverlayItem createItem(int i) {
	  return this.mOverlays.get(i);
	}
	
	@Override
	public int size() {
	  return this.mOverlays.size();
	}
	
	@Override
	protected boolean onTap(int index) {
	  if(this.mContext != null && this.mOverlays.size() > index) {
		  OverlayItem item = this.mOverlays.get(index);
		  AlertDialog.Builder dialog = new AlertDialog.Builder(this.mContext);
		  dialog.setTitle(item.getTitle());
		  dialog.setMessage(item.getSnippet());
		  dialog.show();
	  }
	  return true;
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		Projection projection = mapView.getProjection();
		int mRadius = 5;
		int rad = 5;

		if (shadow == false && location != null) {
			// Get the current location
			Double latitude = location.getLatitude() * 1E6;
			Double longitude = location.getLongitude() * 1E6;
			GeoPoint geoPoint;
			geoPoint = new GeoPoint(latitude.intValue(), longitude.intValue());

			// Convert the location to screen pixels
			Point point = new Point();
			projection.toPixels(geoPoint, point);

			RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
					point.x + mRadius, point.y + mRadius);

			// Setup the paint
			Paint paint = new Paint();
			paint.setARGB(250, 0, 0, 0);
			paint.setAntiAlias(true);
			paint.setFakeBoldText(true);

			Paint backPaint = new Paint();
			backPaint.setARGB(175, 50, 50, 50);
			backPaint.setAntiAlias(true);

			RectF backRect = new RectF(point.x + 2 + mRadius, point.y - 3
					* mRadius, point.x + 65, point.y + mRadius);

			// Draw the marker
			canvas.drawOval(oval, paint);
			canvas.drawRoundRect(backRect, 5, 5, backPaint);
			canvas.drawText("Here I Am", point.x + 2 * mRadius, point.y, paint);

			Paint paintGS = new Paint();
			paintGS.setARGB(250, 255, 0, 0);
			paintGS.setAntiAlias(true);
			paintGS.setFakeBoldText(true);
			for (GasStation gasStation : gasStations) {

				Double lat = gasStation.getLocation().getLatitude() * 1E6;
				Double lng = gasStation.getLocation().getLongitude() * 1E6;

				GeoPoint geoPointGS = new GeoPoint(lat.intValue(), lng.intValue());

				Point myPoint = new Point();
				projection.toPixels(geoPointGS, myPoint);

				RectF ovalGS = new RectF(myPoint.x - rad, myPoint.y - rad,
						myPoint.x + rad, myPoint.y + rad);

				canvas.drawOval(ovalGS, paintGS);
			}
		}
		super.draw(canvas, mapView, shadow);
	}

}
